package mysql

import (
	"Bluebell/models"
	"golang.org/x/crypto/bcrypt"
)

func CreateUser(user *models.User) (err error) {
	hasedPassword, err := bcrypt.GenerateFromPassword([]byte(user.Password), bcrypt.DefaultCost)
	user.Password = string(hasedPassword)
	err = DB.Create(user).Error
	return
}

func GetUserByUsername(username string) (user *models.User, err error) {
	user = new(models.User)
	if err = DB.Debug().Where("username=?", username).First(user).Error; err != nil {
		return nil, err
	}
	return
}

func GetUserByID(id uint64) (user *models.User, err error) {
	user = new(models.User)
	if err = DB.Debug().Where("user_id=?", id).First(user).Error; err != nil {
		return nil, err
	}
	return
}

func IsUserExist(username string) bool {
	var user models.User
	DB.Where("username=?", username).First(&user)
	if user.UserID != 0 {
		return true
	}
	return false
}
